package com.zhang.ShellSort;

/**
 * https://www.runoob.com/w3cnote/shell-sort.html
 *
 * @author Ryan.Li
 * @date 2022/1/24
 **/
public class TestShellSort {
    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 2, 4};
        shellSort(arr);
        for (int value : arr) {
            System.out.println("value = " + value);
        }
    }

    /**
     * 希尔排序
     *
     * @param arr
     */
    public static void shellSort(int[] arr) {
        int length = arr.length;
        int temp;
        for (int step = length / 2; step >= 1; step /= 2) {
            for (int i = step; i < length; i++) {
                temp = arr[i];
                int j = i - step;
                while (j >= 0 && arr[j] > temp) {
                    arr[j + step] = arr[j];
                    j -= step;
                }
                arr[j + step] = temp;
            }
        }
    }
}
